package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

//插入操作
public class addupData {
   public static void addupdata(String url,String user,String password)
   {
       try {
           // 加载并注册JDBC驱动
           Class.forName("com.mysql.cj.jdbc.Driver");
           // 建立到数据库的连接
           Connection conn = DriverManager.getConnection(url, user, password);
           String sql = "INSERT INTO first_table (item_name, Value) VALUES (?, ?)";
           // 创建PreparedStatement对象
           PreparedStatement pstmt = conn.prepareStatement(sql);
           // 设置参数值
           pstmt.setString(1, "电脑"); // 第一个问号的值
           pstmt.setString(2, "5000"); // 第二个问号的值
           // 执行插入操作
           int rowsAffected = pstmt.executeUpdate();
           if (rowsAffected > 0) {
               System.out.println("A new row has been inserted successfully!");
           }

           // 关闭资源
           pstmt.close();
           conn.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

}
